#!/bin/sh
#---------------------------------------------
#   xdg-terminal
#
#   Utility script to open the registered terminal emulator
#
#   Refer to the usage() function below for usage.
#
#   Copyright 2009-2010, Fathi Boudra <fabo@freedesktop.org>
#   Copyright 2009-2010, Rex Dieter <rdieter@fedoraproject.org>
#   Copyright 2006, Kevin Krammer <kevin.krammer@gmx.at>
#
#   LICENSE:
#
#---------------------------------------------

manualpage()
{
cat << '_MANUALPAGE'
_MANUALPAGE
}

usage()
{
cat << '_USAGE'
_USAGE
}

#@xdg-utils-common@

terminal_kde()
{
    if [ -n "$KDE_SESSION_VERSION" ] && [ "$KDE_SESSION_VERSION" -ge 5 ]; then
        local kreadconfig=kreadconfig$KDE_SESSION_VERSION
    else
        local kreadconfig=kreadconfig
    fi

    if which $kreadconfig >/dev/null 2>&1; then
        local terminal=$($kreadconfig --file kdeglobals \
                                      --group General \
                                      --key TerminalApplication \
                                      --default konsole)
    else
        exit_failure_operation_impossible "'$kreadconfig' was not found or is not executable"
    fi

    terminal_exec=`command -v $terminal`

    if [ -x "$terminal_exec" ]; then
        if [ x"$1" = x"" ]; then
            $terminal_exec
        else
            $terminal_exec -e "$1"
        fi

        if [ $? -eq 0 ]; then
            exit_success
        else
            exit_failure_operation_failed
        fi
    else
        exit_failure_operation_impossible "configured terminal program '$terminal' not found or not executable"
    fi
}

terminal_gnome()
{
    term_exec_key="/desktop/gnome/applications/terminal/exec"
    term_exec_arg_key="/desktop/gnome/applications/terminal/exec_arg"

    term_exec=`gconftool-2 --get ${term_exec_key}`
    term_exec_arg=`gconftool-2 --get ${term_exec_arg_key}`

    terminal_exec=`command -v $term_exec`

    if [ -x "$terminal_exec" ]; then
        if [ x"$1" = x"" ]; then
            $terminal_exec
        else
            case "$term_exec_arg" in
              "")
                "$terminal_exec" "$1"
                ;;
              *-x*)
                "$terminal_exec" "$term_exec_arg" sh -c "$1"
                ;;
              *)
                "$terminal_exec" "$term_exec_arg" "$1"
              ;;
            esac
        fi

        if [ $? -eq 0 ]; then
            exit_success
        else
            exit_failure_operation_failed
        fi
    else
        exit_failure_operation_impossible "configured terminal program '$term_exec' not found or not executable"
    fi
}

terminal_gsettings()
{
    term_schema="$1"; shift

    term_exec=`gsettings get ${term_schema} exec | sed -r "s/^'(.*)'$/\1/"`
    term_exec_arg=`gsettings get ${term_schema} exec-arg | sed -r "s/^'(.*)'$/\1/"`

    terminal_exec=`command -v $term_exec`

    if [ -x "$terminal_exec" ]; then
        if [ x"$1" = x"" ]; then
            "$terminal_exec"
        else
            case "$term_exec_arg" in
              "")
                "$terminal_exec" "$1"
                ;;
              *-x*)
                "$terminal_exec" "$term_exec_arg" sh -c "$1"
                ;;
              *)
                "$terminal_exec" "$term_exec_arg" "$1"
              ;;
            esac
        fi

        if [ $? -eq 0 ]; then
            exit_success
        else
            exit_failure_operation_failed
        fi
    else
        exit_failure_operation_impossible "configured terminal program '$term_exec' not found or not executable"
    fi
}

terminal_xfce()
{
    if [ x"$1" = x"" ]; then
        exo-open --launch TerminalEmulator
    else
        exo-open --launch TerminalEmulator "$1"
    fi

    if [ $? -eq 0 ]; then
        exit_success
    else
        exit_failure_operation_failed
    fi
}

terminal_generic()
{
    # if $TERM is a known non-command, use hard-coded fallbacks
    if [ x"$TERM" = x"" ] || [ x"$TERM" = x"linux" ] || [ x"$TERM" = x"vt100" ]; then
        TERM=xterm
    fi

    terminal_exec=`command -v $TERM`

    if [ -x "$terminal_exec" ]; then
        if [ x"$1" = x"" ]; then
            $terminal_exec
        else
            # screen and urxvt won't do their own parsing of quoted arguments
            if [ x"$TERM" = x"screen" ]; then
                # screen has an incompatible meaning for -e
                sh -c "exec $terminal_exec $1"
            elif [ x"$TERM" = x"urxvt" ] || [ x"$TERM" = x"rxvt-unicode" ] || [ x"$TERM" = x"rxvt" ]; then
                #TODO: Use whatever mechanism dash supports to test for
                #      rxvt-* to match things like rxvt-unicode-256color
                sh -c "exec $terminal_exec -e $1"
            else
                $terminal_exec -e "$1"
            fi
        fi

        if [ $? -eq 0 ]; then
            exit_success
        else
            exit_failure_operation_failed
        fi
    else
        exit_failure_operation_impossible "configured terminal program '$TERM' not found or not executable"
    fi
}

terminal_lxde()
{
    if command -v lxterminal >/dev/null; then
        if [ x"$1" = x"" ]; then
            lxterminal
        else
            lxterminal -e "$1"
        fi
    else
        terminal_generic "$1"
    fi
}

terminal_lxqt()
{
    if command -v qterminal >/dev/null; then
        if [ x"$1" = x"" ]; then
            qterminal
        else
            qterminal -e "$1"
        fi
    else
        terminal_generic "$1"
    fi
}

terminal_enlightenment()
{
    if command -v terminology >/dev/null; then
        if [ x"$1" = x"" ]; then
            terminology
        else
            terminology -e "$1"
        fi
    else
        terminal_generic "$1"
    fi
}

#[ x"$1" != x"" ] || exit_failure_syntax

command=
while [ $# -gt 0 ] ; do
    parm="$1"
    shift

    case "$parm" in
      -*)
        exit_failure_syntax "unexpected option '$parm'"
        ;;

      *)
        if [ -n "$command" ] ; then
            exit_failure_syntax "unexpected argument '$parm'"
        fi
        command="$parm"
        ;;
    esac
done

detectDE

if [ x"$DE" = x"" ]; then
    DE=generic
fi

case "$DE" in
    kde)
    terminal_kde "$command"
    ;;

    gnome)
    terminal_gnome "$command"
    ;;

    gnome3)
    terminal_gsettings org.gnome.desktop.default-applications.terminal "$command"
    ;;

    cinnamon)
    terminal_gsettings org.cinnamon.desktop.default-applications.terminal "$command"
    ;;

    mate)
    terminal_gsettings org.mate.applications-terminal "$command"
    ;;

    xfce)
    terminal_xfce "$command"
    ;;

    lxde)
    terminal_lxde "$command"
    ;;

    lxqt)
    terminal_lxqt "$command"
    ;;

    enlightenment)
    terminal_enlightenment "$command"
    ;;

    generic)
    terminal_generic "$command"
    ;;

    *)
    exit_failure_operation_impossible "no terminal emulator available"
    ;;
esac
